package gov.child.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;

import static java.util.stream.Collectors.toList;

/**
 * @author fzh
 * @since 2017/9/12
 */
public class AnnotationHelper {
    public static <T> Collection<Field> annotatedFields(Class<T> cls, Class<? extends Annotation> clazz) {
        return Arrays.stream(cls.getDeclaredFields())
                .filter(field -> field.isAnnotationPresent(clazz))
                .collect(toList());
    }

    public static <T> Collection<Field> unAnnotatedFields(Class<T> cls, Class<? extends Annotation> clazz) {
        return Arrays.stream(cls.getDeclaredFields())
                .filter(field -> !field.isAnnotationPresent(clazz))
                .collect(toList());
    }

    public static <T> Collection<Method> annotatedMethods(Class<T> cls, Class<? extends Annotation> clazz) {
        return Arrays.stream(cls.getDeclaredMethods())
                .filter(method -> method.isAnnotationPresent(clazz))
                .collect(toList());
    }

    public static <T> Collection<Method> unAnnotatedMethods(Class<T> cls, Class<? extends Annotation> clazz) {
        return Arrays.stream(cls.getDeclaredMethods())
                .filter(method -> !method.isAnnotationPresent(clazz))
                .collect(toList());
    }
}
